//
// Created by Martin on 2021/9/29.
//

#include "CashContext.h"
#include "CashSuper.h"
#include "CashNormal.h"
#include "CashRebate.h"
#include "CashReturn.h"
#include <iostream>
#include <algorithm>

using namespace std;

/**
 * 策略模式
 * 应用示例: 商场促销打折
 * @see https://www.cnblogs.com/adamjwh/p/11011095.html
 */
int main()
{
    CashContext *context = nullptr;

    CashSuper *cashStrategy = nullptr;
    bool exit_loop = false;

    while (!exit_loop) {
        cout << "请输入打折方式(1/2/3/-1)：\n1. 正常销售，不打折；\n2. 8折；\n3. 满300减100；\n-1. 结束；" << endl;
        int discount;
        cin >> discount;

        switch (discount) {
            case -1:
                exit_loop = true;
                break;
            case 1:
                cashStrategy = new CashNormal();
                break;
            case 2:
                cashStrategy = new CashRebate(0.8);
                break;
            case 3:
                cashStrategy = new CashReturn(300, 100);
                break;

            default:
                ;
        }

        if (!cashStrategy) {
            continue;
        }

        double price, totalPrice, finalPrice;
        int num;
        cout << "输入单价: " << endl;
        cin >> price;
        cout << "输入数量: " << endl;
        cin >> num;

        context = new CashContext(cashStrategy);
        totalPrice = price * num;
        finalPrice = context->getResult(totalPrice);
        cout << "单价:" << price << ", 数量:" << num << endl;
        cout << "总计: " << totalPrice << endl;
        cout << "折扣活动: " << cashStrategy->getTitle() << endl;
        cout << "最终价格: " << finalPrice << "\n" << endl;

        cout << "\n继续? (y/n)" << endl;
        string continue_flg;
        cin >> continue_flg;

        transform(continue_flg.begin(), continue_flg.end(), continue_flg.begin(), ::towlower);
        if (continue_flg != "y" && continue_flg != "yes") {
            exit_loop = true;
        }

        delete context;
        delete cashStrategy;
        context = nullptr;
        cashStrategy = nullptr;
    }

    cout << "成功退出" << endl;
    return 0;
}
